-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: pd.Series.shift and .diff to accept a collection of numbers #44660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
skwirskj
commented
Nov 28, 2021
•
edited
Loading
edited
- closes ENH: pd.Series.shift and .diff to accept a collection of numbers #44424
- tests added / passed
- Ensure all linting tests pass, see here for how to run them
- whatsnew entry
…e shift functions to allow iterables to be passed as the period
…ake in a list for periods and then return a concatenated DataFrame of each consecutive shift in the periods list
…llection Merging the master branch with my changes for Enhancement at issue 44424
Created two new tests to test shifting data in Series and DataFrames with an iterable as the period parameter.
…llection Merging after updating tests and resetting the generic.py file
Reverted some changes that were made when originally finding a solution and then reset it to the latest version on the upstream master branch.
Updated how we are creating the new DataFrame after shifting to improve performance. Also reverted core/generic.py to master
Attempting to fix whatsnew rst file
|
||
Usage within the :class:`DataFrame` class: | ||
|
||
.. ipython:: python |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is fine, but the main source of information is the doc-strings which need updating with examples & prose.
|
||
The :meth:`DataFrame.shift` and :meth:`Series.shift` functions can take in an iterable, such as a list, for the period parameter. When an iterable is passed | ||
to either function it returns a :class:`DataFrame` object with all of the shifted rows or columns concatenated with one another. | ||
The function applies a shift designated by each element in the iterable. The resulting :class:`DataFrame` object's columns will retain the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need this sentence to the end. you can say that the suffix
parameter controls the names but don't have to fully explain here. that's the point of the doc-string.
) -> DataFrame: | ||
axis = self._get_axis_number(axis) | ||
|
||
# GH#44424 Handle the case of multiple shifts |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this comment is not needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if axis==1 this should fail
for i in periods: | ||
if not isinstance(i, int): | ||
raise TypeError( | ||
f"Value {i} in periods is not an integer, expected an integer" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this have tests?
# GH#44424 Handle the case of multiple shifts | ||
if is_list_like(periods): | ||
|
||
new_df = DataFrame() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is superfluous
|
||
from pandas.core.reshape.concat import concat | ||
|
||
new_df_list = [] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use result and not new_df
) -> Series | DataFrame: | ||
# Handle the case of multiple shifts | ||
if is_list_like(periods): | ||
if len(periods) == 0: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this tested? this should be an error i think
if len(periods) == 0: | ||
return self | ||
|
||
df = self.to_frame() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are you doing this? shouldn't you just be concatting the result series?
@@ -429,3 +429,23 @@ def test_shift_axis1_categorical_columns(self): | |||
columns=ci, | |||
) | |||
tm.assert_frame_equal(result, expected) | |||
|
|||
def test_shift_with_iterable(self): | |||
# GH#44424 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about error conditions
@@ -376,3 +377,15 @@ def test_shift_non_writable_array(self, input_data, output_data): | |||
expected = Series(output_data, dtype="float64") | |||
|
|||
tm.assert_series_equal(result, expected) | |||
|
|||
def test_shift_with_iterable(self): | |||
# GH#44424 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same question
This pull request is stale because it has been open for thirty days with no activity. Please update and respond to this comment if you're still interested in working on this. |
closing as stale if you want continue, pls ping |
sure |